package com.zzh.test;
  
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 

 

public class WebServiceClient {
	private Logger logger = LoggerFactory.getLogger(WebServiceClient.class);
	private Client client; 
	private static WebServiceClient webclient = new WebServiceClient();
	
	
	private WebServiceClient(){
		logger.info("webservice构造器------------------------------------"); 
		try{ 
			  
				DynamicClientFactory factory = DynamicClientFactory.newInstance();
				this.client = factory.createClient("http://101.200.185.99:9002/ParseServer/ServiceStart?wsdl");
			 
		}catch(Exception e){
			logger.info("错误的启动构造器"+e.getMessage());
		}
	}
	
	public static WebServiceClient getClient(){
		return webclient;
	}
	/**
	 * invoke,调用webservice接口返回结果.
	 * @param typeClass 需要返回的类型
	 * @param wsdlUrl 接口的wsdl地址
	 * @param methodName 方法名称
	 * @param objects 参数数组
	 * @return
	 */
	public<T> T  invoke01(String methodName,Object...objects) throws Exception{
		T result = null;
		try{
			String json = invoke(methodName, objects);  //这一步json数据已经返回   methodName="parseResume"
			if(null != json&&!json.equals("")){
				//去除id:0字段
				json = json.replaceAll(",\"id\":0,",",,");
				json = json.replaceAll(",,",",");
				System.out.println("json==================" +json );
				//result=mapper.readValue(json, typeClass);
			}
		}catch(Exception e){
			throw new Exception("error:" + e.getMessage() , e);
		}
		return result;
	}
	/**
	 * 调用webservice接口的方法
	 * @param methodName
	 * @param objects
	 * @return
	 * @throws Exception
	 */
	public String invoke(String methodName,Object...objects) throws Exception{
		logger.info("webservice 准备启动客户端...............................");
		String result = null;
		try{
			if(null != this.client){
				logger.info("webservice 客户端启动成功...............................");
				Object[] res=client.invoke(methodName,objects);
				logger.info("webservice 客户端返回结果..............................."+res);
				if(res != null && res[0] != null&&!res[0].equals("")){
					String json = res[0].toString();
 					result=res[0].toString();
				}
			}
		}catch(Exception e){
			throw new Exception("error:" + e.getMessage() , e);
		}
		return result;
	}
	
	
	public static void main(String[] args) throws Exception {
		
		WebServiceClient client = new WebServiceClient();
		String path = "/usr/local/apache-tomcat-8.5.4/webapps/honestcareer/static/file/upload/attach/talent/18843142689/1484036283931.doc";
		
		client.invoke01("parseResume", path);
		
		
	}
}
